//
//  NearbyTableView.m
//  BaiTuan
//
//  Created by 看着太阳吃烧饼 on 15/12/15.
//  Copyright © 2015年 YR. All rights reserved.
//

#import "NearbyTableView.h"
#import "SectionHeaderView.h"
#import "DealTableViewCell.h"
#import "DealDetailViewController.h"
#import "UIView+NavigationController.h"

#define kCellIdentifier @"DealCell"


@implementation NearbyTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
        [self registerNib:[UINib nibWithNibName:@"DealTableViewCell" bundle:nil] forCellReuseIdentifier:kCellIdentifier];
        self.tableHeaderView = [self createHeaderView];

    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:UITableViewStylePlain];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
        [self registerNib:[UINib nibWithNibName:@"DealTableViewCell" bundle:nil] forCellReuseIdentifier:kCellIdentifier];
        self.tableHeaderView = [self createHeaderView];
    }
   
    return self;
}

- (UIView *)createHeaderView
{
    //创建组头视图
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
    bgView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    
    //组头文字：当前路段
    UILabel *locationLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 5, kScreenWidth - 50, 20)];
    locationLabel.backgroundColor = [UIColor clearColor];
    locationLabel.text = @"当前:滨江区文涛路";
    locationLabel.textColor = [UIColor grayColor];
    locationLabel.font = [UIFont systemFontOfSize:13];
    
    [bgView addSubview:locationLabel];
    
    //组头刷新按钮
    UIButton *refreshButton = [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth - 40, 5, 20, 20)];
    [refreshButton setImage:[UIImage imageNamed:@"icon_dellist_locate_refresh.png"] forState:UIControlStateNormal];
    [bgView addSubview:refreshButton];
    return bgView;
}


#pragma mark - tableView delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    ShopsModel *shopsModel = _dataArray[section];
    return [shopsModel.deal_num integerValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DealTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
    ShopsModel *shopsModel = _dataArray[indexPath.section];
    NSArray *dealArray = [DealModel dealsWithDic:shopsModel.deals];
    cell.deal = dealArray[indexPath.row];
    cell.distance.hidden = YES;
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 70;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 20;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    SectionHeaderView *headerView = [[SectionHeaderView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 70)];
    headerView.shopsModel = _dataArray[section];
    return headerView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShopsModel *shopsModel = _dataArray[indexPath.section];
    NSArray *dealArray = [DealModel dealsWithDic:shopsModel.deals];
    DealDetailViewController *dealVC = [[DealDetailViewController alloc]init];
    dealVC.deal = dealArray[indexPath.row];
    dealVC.title = [dealArray[indexPath.row] title];
    NSArray *subViews = self.navigationController.navigationBar.subviews;
    for (UIView *view in subViews) {
        if ([view isKindOfClass:[UISearchBar class]]) {
            view.hidden = YES;
        }
    }
    [self.navigationController pushViewController:dealVC animated:YES];
}

@end
